package com.cc.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.cc.allreturn.AjaxReturnBean;
import com.cc.pojo.dto.DataBaseDto;
import com.cc.utils.JDBCUtils;
import com.cc.utils.beanUtil.BeanUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
 * `
 *
 * @User c.c
 * @Since 2023/5/12
 * @Time 15:37
 * @Description
 */

@Controller
public class UpdateDataBaseController {

  @Resource
  private ContextRefresher contextRefresher;

  @PostMapping("/api/changeyaml")
  @ResponseBody
  public AjaxReturnBean<String> testchangeyaml(@RequestBody DataBaseDto dataBaseDto) throws SQLException {
    if (StringUtils.isBlank(dataBaseDto.getUser())) {
      throw new RuntimeException("用户信息不能为空");
    } else if (StringUtils.isBlank(dataBaseDto.getPassword())) {
      throw new RuntimeException("密码不能为空");
    } else if (StringUtils.isBlank(dataBaseDto.getAddress())) {
      throw new RuntimeException("地址不能为空");
    }
    DruidDataSource dataSource = BeanUtils.getBean(DruidDataSource.class);
    dataSource.restart();
    JDBCUtils.reload(dataBaseDto.getAddress(), dataBaseDto.getUser(), dataBaseDto.getPassword(), dataBaseDto.getDatabasename());
//    byUpdateTargetmethod(dataBaseDto);


    //查看是否连接成功
    if (JDBCUtils.isConnection()) {
      return AjaxReturnBean.createSuccess("连接成功");
    } else {
      return AjaxReturnBean.createError("连接失败，请重试");
    }
  }

  private void byUpdateTargetmethod(DataBaseDto dataBaseDto) throws IOException, SQLException {
    InputStream applicationStream = this.getClass().getClassLoader().getResourceAsStream("application.yaml");
    Yaml yaml = new Yaml();
    Map<String, Object> yamlMap = yaml.load(applicationStream);
    System.out.println(yamlMap);


    yamlMap.put("address", dataBaseDto.getAddress());
    yamlMap.put("databasename", dataBaseDto.getDatabasename());
    yamlMap.put("password", dataBaseDto.getPassword());
    yamlMap.put("account", dataBaseDto.getUser());


    String path = this.getClass().getClassLoader().getResource("application.yaml").getPath();
    //字符输出
    FileWriter fileWriter = new FileWriter(path);
    //用yaml方法把map结构格式化为yaml文件结构
    fileWriter.write(yaml.dumpAsMap(yamlMap));
    //刷新
    fileWriter.flush();
    //关闭流
    fileWriter.close();


    DruidDataSource dataSource = BeanUtils.getBean(DruidDataSource.class);
    dataSource.restart();
    //刷新配置文件
    contextRefresher.refresh();
  }
}
